using System.Collections;
using System.Collections.Generic;

namespace ColladaDotNet.SimpleMesh
{
	public class BonesMap: IEnumerable<Bone>
	{
		List<Bone> list = new List<Bone>();

		readonly Dictionary<string, Bone> map = new Dictionary<string, Bone>();

		public BonesMap()
		{
			
		}

		public Bone this[string name]
		{
			get
			{
				Bone buf;
				if (!map.TryGetValue(name, out buf))
					return null;
				return buf;
			}
			set
			{
				Bone buf;
				if (!map.TryGetValue(name, out buf))
				{
					value.Index = (ulong)list.Count;
					list.Add(value);
					map[name] = value;
				}
				else
					buf.Copy(value);
			}
		}

		/// <summary>
		/// Returns an enumerator that iterates through the collection.
		/// </summary>
		/// <returns>
		/// A <see cref="T:System.Collections.Generic.IEnumerator`1"></see> that can be used to iterate through the collection.
		/// </returns>
		/// <filterpriority>1</filterpriority>
		public IEnumerator<Bone> GetEnumerator()
		{
			return list.GetEnumerator();
		}

		/// <summary>
		/// Returns an enumerator that iterates through a collection.
		/// </summary>
		/// <returns>
		/// An <see cref="T:System.Collections.IEnumerator"></see> object that can be used to iterate through the collection.
		/// </returns>
		/// <filterpriority>2</filterpriority>
		IEnumerator IEnumerable.GetEnumerator()
		{
			return GetEnumerator();
		}

		public void Scale(double factor)
		{
			foreach (var bone in list)
			{
				bone.ScalePosition(factor);
			}
		}
	}
}